/**
 * This file is part of camera-motion-timer.
 *
 *  camera-motion-timer is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  camera-motion-timer is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *   along with camera-motion-timer.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <LiquidCrystal.h>
#include <Stepper.h>

// Botones
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

// FIXME Cambiar pines para que no se solapen
const int IR_pin = 8;
const int buttonPin = 2;

// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

// read the buttons
int read_LCD_buttons() {
 int adc_key_in = analogRead(0);      // read the value from the sensor 
 // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
 // we add approx 50 to those values and check to see if we are close
 if (adc_key_in > 1000) 
   return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
 if (adc_key_in < 50)   
   return btnRIGHT;  
 if (adc_key_in < 195)  
   return btnUP; 
 if (adc_key_in < 380)  
   return btnDOWN; 
 if (adc_key_in < 555)  
   return btnLEFT; 
 if (adc_key_in < 790)  
   return btnSELECT;   
 return btnNONE;  // when all others fail, return this...
}

/**
 * Toma una foto empleando la secuencia de pulsos.
 * Al final, vuelve al estado de reposo.
 */
void takeDigitalShot() {
    digitalWrite(IR_pin,HIGH);
    delayMicroseconds(2250);
    //Actualmente, el valor más grande producirá un 
    //retraso exacto es 16383. Esto puede cambiar en una
   // futura versión de Arduino. Para retrazos más largos que 
   //algunos miles de microsegundos, deberías usar delay() . 
    digitalWrite(IR_pin,LOW);
    delayMicroseconds(27600);
    digitalWrite(IR_pin,HIGH);
    delayMicroseconds(650);
    digitalWrite(IR_pin,LOW);
    delayMicroseconds(1375);
    digitalWrite(IR_pin,HIGH);
    delayMicroseconds(575);
    digitalWrite(IR_pin,LOW);
    delayMicroseconds(3350);
    digitalWrite(IR_pin,HIGH);
    delayMicroseconds(650);
    digitalWrite(IR_pin,LOW);
    
    /* Parece ser necesario que la anterior secuencia de
    pulsos se repita despues de 63ms*/
    delay(63);
    
    digitalWrite(IR_pin,HIGH);
    delayMicroseconds(2250); 
    digitalWrite(IR_pin,LOW);
    delayMicroseconds(27600);
    digitalWrite(IR_pin,HIGH);
    delayMicroseconds(650);
    digitalWrite(IR_pin,LOW);
    delayMicroseconds(1375);
    digitalWrite(IR_pin,HIGH);
    delayMicroseconds(575);
    digitalWrite(IR_pin,LOW);
    delayMicroseconds(3350);
    digitalWrite(IR_pin,HIGH);
    delayMicroseconds(650);
    digitalWrite(IR_pin,LOW);
    // Estado de reposo
    digitalWrite(IR_pin,LOW);
}


void setup() {
  // Inicia el serie
  Serial.begin(9600);
  // Configura los pines
  pinMode(IR_pin,OUTPUT);
  pinMode(buttonPin,INPUT);
  digitalWrite(IR_pin,LOW);
  // Inicia el LCD
  lcd.begin(16, 2);
  lcd.setCursor(0,0);
}

/* Hacer el disparo dependiente del boton.*/

void loop(){
  
  lcd.setCursor(0,1);            // move to the begining of the second line

 switch (read_LCD_buttons()) {
   case btnRIGHT:
     {
     lcd.print("RIGHT ");
     break;
     }
   case btnLEFT:
     {
     lcd.print("LEFT   ");
     break;
     }
   case btnUP:
     {
     lcd.print("UP    ");
     break;
     }
   case btnDOWN:
     {
     lcd.print("DOWN  ");
     break;
     }
   case btnSELECT:
     {
     lcd.print("SELECT");
     takeDigitalShot();
     break;
     }
     case btnNONE:
     {
     lcd.print("NONE  ");
     break;
     }
 }
}
